/**
 * Created by liuyunfei on 2016-12-30.
 */
module.exports = function ($rootScope,$state,$scope,userInfoService,$cookies) {

    $scope.inputFYPlaceInput = '';
    $scope.lawfirmid = '';
    /*用户Id和pwd的默认缓存时间*/
    var expireDate = new Date();
    expireDate.setDate(expireDate.getDate() + 1);

    /**
     * 完善用户信息
     */
    $scope.updateUserInfo = function(){
        $scope.id = kbt.decrypt($cookies.get('cookieUserId'));
        $scope.entryyear = $('.dfg').val();


        if(!$scope.username){
            bootbox.alert({
                size: "small",
                title: "提示信息",
                message: "请填写真实姓名！",
                callback: function(){ /* your callback code */ }
            });
            return;
        }
        if($("#lawFirm").html == "请选择执业律所"){
            bootbox.alert({
                size: "small",
                title: "提示信息",
                message: "请选择执业律所！",
                callback: function(){ /* your callback code */ }
            });
            return;
        }
        if(!$scope.entryyear){
            bootbox.alert({
                size: "small",
                title: "提示信息",
                message: "请填写入职年份！",
                callback: function(){ /* your callback code */ }
            });
            return;
        }
        if(!$scope.certificateno){
            bootbox.alert({
                size: "small",
                title: "提示信息",
                message: "请填写执业证号！",
                callback: function(){ /* your callback code */ }
            });
            return;
        }
        else {
            if(!validate($scope.certificateno) && $scope.certificateno.length != 17){
                bootbox.alert({
                    size: "small",
                    title: "提示信息",
                    message: "有效执业证号必须为17位数字！",
                    callback: function(){ /* your callback code */ }
                });
                return;
            }
        }

        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;

        userInfoService.updateUserInfo($scope.id,$scope.username,$scope.lawfirmid,$scope.entryyear,$scope.certificateno).then(function (data) {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
            if (data.code == 1){
                $cookies.put('userPortraitName',$scope.username,{
                    expires:expireDate
                });

                $cookies.put('cookieUserName', kbt.encrypt($scope.username), {
                    expires: expireDate
                });
                $cookies.put('cookieIsUpdated', 1, {
                    expires: expireDate
                });
                $scope.headImageUrl = data.user.iconUrl;
                //刷新整个界面
                window.location.reload();
                $state.go('workBarNoWork');

                $('#Bar').children('li:eq(0)').addClass('active').siblings('li').removeClass('active');
            } else {
                bootbox.alert({
                    size: "small",
                    title: "提示信息",
                    message: "操作失败！",
                    callback: function(){ /* your callback code */ }
                });
                return;
            }
        }, function () {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
        });

    };

    /**
     * 获取律所列表
     */
    $scope.getLawFirms = function(){
        if(!$scope.lawFirms){
            userInfoService.getLawFirms().then(function (data) {
                if (data.code == 1){
                    $scope.lawFirms = data.data;
                }
            });
        }
    };

    $scope.getLawFirms();

    $scope.showFYName = function () {
        $('.lsSearchName').css('display','block');
    }

    /**
     * 律所选择下拉框
     */
    $scope.selectFYName = function(name,id){
        $scope.lawfirmid = id;
        $scope.lawfirmName = name;
        $scope.inputFYPlaceInput = name;
        //$("#lawFirm").html($scope.lawfirmName);
        $('.lsSearchName').css('display','none');
    };


    function checkPhoneIsRight() {
        var phone = $scope.cellphone;
        if (!(/^1[34578]\d{9}$/.test(phone))) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 日期选择器
     */
    $(".form_datetime").datetimepicker({
        minView: "month",
        format: 'yyyy-mm-dd',
        language: "zh-CN",
        charset:"utf-8"
    });

    $('.form_datetime')
        .datetimepicker()
        .on('changeDate', function(ev){
            $('.form_datetime').datetimepicker('hide');
        });

    /**
     * 名字如果为三位数，截取后两位
     */
    $("#edit-name").change(function () {
        var name = $("#edit-name").val();
        if (isChinese(name)) {
            var nameSub = name.substr(name.length - 2);
            $(".position_relative").html(nameSub);
        }else{
            $(".position_relative").html(name);
        }
    });

    /**
     * 判断是否为中文
     * @param temp
     * @returns {boolean}
     */
    function isChinese(temp) {
        var re = /[^\u4e00-\u9fa5]/;
        if (re.test(temp)) return false;
        return true;
    }

    $scope.init = function(){

    };

    $scope.init();
};
